package com.zyz.K_集合.Map实现类四HashTable;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
 * @author: vick
 * @Date: 2025/3/31
 * @Description:
 * @version: 1.0
 */
public class Demo2 {
    //线程数量
    private static final int THREAD_COUNT = 10;
    //每个线程对map进行读写的次数
    private static final int COUNT = 1000;
    public static void main(String[] args) throws InterruptedException {
        testMap(new HashMap());
        testMap(new Hashtable());
    }

    private static void testMap(Map map) throws InterruptedException {
        Thread[] threads = new Thread[THREAD_COUNT];

        //每个线程都会对传入的map进行多次put和get操作
        for (int i = 0; i < THREAD_COUNT; i++) {
            threads[i] = new Thread(() -> {
                for (int j = 0; j < COUNT; j++) {
                    map.put(j, j);
                    map.get(j);
                }
            });
            threads[i].start();
        }

        for (Thread thread : threads) {
            thread.join();
        }

        String simpleName = map.getClass().getSimpleName();
        System.out.println(simpleName + " 最终大小： " + map.size());
    }
}
